<?php
/**
 * creater: 
 * date: 
 * Class: Thông tin đăng nhập/Đăng xuất
 */
class IndexController extends  Zend_Controller_Action {
	public function init(){
		//Load cau hinh thu muc trong file config.ini
        $app = Zend_Registry::get('App');
		//Cau hinh cho Zend_layout
		Zend_Layout::startMvc(array(
			    'layoutPath' => $app->layout,
			    'layout' => 'login'
			    ));
		//Lay duong dan thu muc goc (path directory root)
		$this->view->baseUrl = $this->_request->getBaseUrl() . "/public/";
	}

	public function indexAction(){

		$auth = Zend_Auth::getInstance();
        if ($auth->hasIdentity()) {
            $this->_redirect("module/receive/index");
        }
        $user = $this->_request->getParam('username');
		$pass = $this->_request->getParam('Passwd');
		$persistentCookie  = $this->_request->getParam('PersistentCookie');
		$pass = md5($pass);
		$muser = new models_Login;
        if($muser->check_login($user,$pass) == FALSE){
                $this->view->error="Wrong username or password";
        }else{
        	// $identity = Zend_Auth::getInstance()->getIdentity();
        	if($persistentCookie)
        		$this->setCookie(array('username'=>$user,'Passwd'=>$pass));
        	$this->_redirect("module/receive/index");
        }
        $this->view->user = $user;
    }
    public function logoutAction(){
        $muser = new models_Login;
        $muser->logout();
        $this->_helper->viewRenderer->setNoRender();    
        $this->_redirect("index");        
    }
    public function preDispatch() {
	    $auth = Zend_Auth::getInstance();
	    if (!$auth->hasIdentity()) {
	        if ($this->_request->getActionName() != 'index') {
	        	$this->_redirect('/index/');
	        }
	    }
	}
	public function setCookie($data){

	    $username_cookie = new Zend_Http_Cookie('username', $data['username'], $_SERVER['HTTP_HOST'], '', FALSE);
		$pass_cookie = new Zend_Http_Cookie('Passwd', $data['Passwd'], $_SERVER['HTTP_HOST'], '', FALSE);
	    $cookie_jar = new Zend_Http_CookieJar();
	    $cookie_jar->addCookie($username_cookie);
	    $cookie_jar->addCookie($pass_cookie);
	}
	public function forgotpasswordAction(){
		echo 12132; die;
	}
}
?>